package com.changgou.goods.service.impl;

import com.changgou.goods.dao.AlbumMapper;
import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

/**
 * @author BAO
 * @version V1.0
 * @Package com.changgou.goods.service.impl
 * @date 2020/12/7 11:55
 * @description:
 */
@Service
public class AlbumServiceImpl implements AlbumService {

    @Autowired
    private AlbumMapper albumMapper;

    @Override
    public List<Album> findAll() {
        return albumMapper.selectAll();
    }

    @Override
    public Album findById(Integer albumId) {
        return albumMapper.selectByPrimaryKey(albumId);
    }

    @Override
    public int save(Album album) {
        return albumMapper.insertSelective(album);
    }

    @Override
    public int updateById(Album album) {
        return albumMapper.updateByPrimaryKeySelective(album);
    }

    @Override
    public int deleteById(Integer albumId) {
        return albumMapper.deleteByPrimaryKey(albumId);
    }

    @Override
    public List<Album> findCondition(Map<String, Object> searchMap) {
        Example example = new Example(Album.class);
        if (searchMap!=null){
            if (searchMap.get("title")!=null&&!"".equals(searchMap.get("title"))){
                example.createCriteria().andLike("title","%"+searchMap.get("title")+"%");
            }
        }
        return albumMapper.selectByExample(example);
    }

    @Override
    public Page<Album> findByPage(int currentPage, int size) {
        PageHelper.startPage(currentPage,size);
        Page<Album> albums = (Page<Album>)albumMapper.selectAll();
        return albums;
    }

    @Override
    public Page<Album> findByPageAndCondition(Map<String, Object> searchMap, int currentPage, int size) {
        Example example = new Example(Album.class);
        if (searchMap!=null){
            if (searchMap.get("title")!=null&&!"".equals(searchMap.get("title"))){
                example.createCriteria().andLike("title","%"+searchMap.get("title")+"%");
            }
        }
        PageHelper.startPage(currentPage,size);
        Page<Album> albums = (Page<Album>)albumMapper.selectByExample(example);
        return albums;
    }
}
